package com.baizhi.service.impl;

import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import com.baizhi.dao.CategoryDao;
import com.baizhi.dao.UserDao;
import com.baizhi.domain.Result;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {
    @Override
    public List<User> selectAllUser() {
        List<User> users=new ArrayList<>();
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            users=userDao.selectAllUser();
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return users;
    }

    @Override
    public void updateUserStatus(User user) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            userDao.updateUserStatus(user);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public User selectUserById(String userId) {
        User user=null;
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            user = userDao.selectUserById(userId);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return user;
    }

    @Override
    public User selectByEmail(String email) {
        User user=null;
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            user = userDao.selectByEmail(email);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return user;
    }


    @Override
    public Result login(String username, String password) {
        Result result=null;
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            User user = userDao.selectByEmail(username);
            if(user==null){
                result=new Result(false,"该用户未注册");
                return result;
            }
            sqlSession.commit();
            if(user.getStatus().equals("禁用")){
                result=new Result(false,"该用户已被禁用");
                return result;
            }
            HMac md5 = new HMac(HmacAlgorithm.HmacMD5,user.getSalt().getBytes());
            if(user.getPassword().equals(md5.digestHex(password))){
                result=new Result(true,"登陆成功",user);
                return result;
            }else {
                result=new Result(false,"密码错误");
                return result;
            }
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public Boolean register(User user) {
        Boolean flag=false;
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            User user1 = userDao.selectByEmail(user.getEmail());
            if (user1!=null){
                return flag;
            } else {
                userDao.addUser(user);
                flag=true;
            }
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return flag;
    }
}
